package com.example.springdataneo4jdemo.controller;

import com.example.springdataneo4jdemo.domain.Person;
import com.example.springdataneo4jdemo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/persons")
public class PersonController {

    private final PersonService personService;

    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    // 新增 Person
    @PostMapping
    public Person addPerson(@RequestBody Person person) {
        return personService.savePerson(person);
    }

    // 查询所有 Person
    @GetMapping
    public List<Person> getAllPersons() {
        return personService.findAllPersons();
    }

    // 按名字查询 Person
    @GetMapping("/name/{name}")
    public List<Person> getPersonsByName(@PathVariable String name) {
        return personService.findByName(name);
    }

    // 删除 Person
    @DeleteMapping("/{id}")
    public void deletePerson(@PathVariable Long id) {
        personService.deletePerson(id);
    }
}